package lsgoodsclass

import (
	"fmt"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/lsgoodsclass"
	lsgoodsclassReq "github.com/flipped-aurora/gin-vue-admin/server/model/lsgoodsclass/request"
	"gorm.io/gorm"
)

type LsGoodsClassService struct{}

// CreateLsGoodsClass 创建lsGoodsClass表记录
// Author [yourname](https://github.com/yourname)
func (lsGoodsClassService *LsGoodsClassService) CreateLsGoodsClass(lsGoodsClass *lsgoodsclass.LsGoodsClass) (err error) {
	err = global.GVA_DB.Create(lsGoodsClass).Error
	return err
}

// DeleteLsGoodsClass 删除lsGoodsClass表记录
// Author [yourname](https://github.com/yourname)
func (lsGoodsClassService *LsGoodsClassService) DeleteLsGoodsClass(ID string, userID uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Model(&lsgoodsclass.LsGoodsClass{}).Where("id = ?", ID).Update("deleted_by", userID).Error; err != nil {
			return err
		}
		if err = tx.Delete(&lsgoodsclass.LsGoodsClass{}, "id = ?", ID).Error; err != nil {
			return err
		}
		return nil
	})
	return err
}

// DeleteLsGoodsClassByIds 批量删除lsGoodsClass表记录
// Author [yourname](https://github.com/yourname)
func (lsGoodsClassService *LsGoodsClassService) DeleteLsGoodsClassByIds(IDs []string, deleted_by uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Model(&lsgoodsclass.LsGoodsClass{}).Where("id in ?", IDs).Update("deleted_by", deleted_by).Error; err != nil {
			return err
		}
		if err := tx.Where("id in ?", IDs).Delete(&lsgoodsclass.LsGoodsClass{}).Error; err != nil {
			return err
		}
		return nil
	})
	return err
}

// UpdateLsGoodsClass 更新lsGoodsClass表记录
// Author [yourname](https://github.com/yourname)
func (lsGoodsClassService *LsGoodsClassService) UpdateLsGoodsClass(lsGoodsClass lsgoodsclass.LsGoodsClass) (err error) {
	err = global.GVA_DB.Model(&lsgoodsclass.LsGoodsClass{}).Where("id = ?", lsGoodsClass.ID).Updates(&lsGoodsClass).Error
	return err
}

// GetLsGoodsClass 根据ID获取lsGoodsClass表记录
// Author [yourname](https://github.com/yourname)
func (lsGoodsClassService *LsGoodsClassService) GetLsGoodsClass(ID string) (lsGoodsClass lsgoodsclass.LsGoodsClass, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&lsGoodsClass).Error
	return
}

// GetLsGoodsClassInfoList 分页获取lsGoodsClass表记录
// Author [yourname](https://github.com/yourname)
func (lsGoodsClassService *LsGoodsClassService) GetLsGoodsClassInfoList(info lsgoodsclassReq.LsGoodsClassSearch) (list []*ReturnLsGoodsClass, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&lsgoodsclass.LsGoodsClass{})
	var lsGoodsClasss []lsgoodsclass.LsGoodsClass
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	//if info.ClassName != nil && *info.ClassName != "" {
	//    db = db.Where("class_name = ?",*info.ClassName)
	//}
	//if info.Enable != nil && *info.Enable != "" {
	//    db = db.Where("enable = ?",*info.Enable)
	//}
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	// var OrderStr string
	// orderMap := make(map[string]bool)
	//  	orderMap["sort"] = true
	//if orderMap[info.Sort] {
	//   OrderStr = info.Sort
	//   if info.Order == "descending" {
	//      OrderStr = OrderStr + " desc"
	//   }
	//   db = db.Order(OrderStr)
	//}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}
	getList := GetList(db, lsGoodsClasss, 0)
	//err = db.Find(&lsGoodsClasss).Error
	fmt.Println(getList)
	return getList, total, err
}
func (lsGoodsClassService *LsGoodsClassService) GetLsGoodsClassPublic() {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}
func GetList(db *gorm.DB, list []lsgoodsclass.LsGoodsClass, pid int) []*ReturnLsGoodsClass {
	var cate []lsgoodsclass.LsGoodsClass
	var sli []*ReturnLsGoodsClass
	global.GVA_DB.Where("pid=?", pid).Find(&cate)
	for _, d := range cate {
		var one []lsgoodsclass.LsGoodsClass
		global.GVA_DB.Where("pid=?", d.ID).Find(&one)
		two := ReturnLsGoodsClass{
			GVA_MODEL: global.GVA_MODEL{
				ID:        d.ID,
				CreatedAt: d.CreatedAt,
				UpdatedAt: d.UpdatedAt,
				DeletedAt: d.DeletedAt,
			},
			ClassName: d.ClassName,
			Pid:       d.Pid,
			ClassIcon: d.ClassIcon,
			Sort:      d.Sort,
			Enable:    d.Enable,
			CreatedBy: d.CreatedBy,
			UpdatedBy: d.UpdatedBy,
			DeletedBy: d.DeletedBy,
			Children:  GetList(db, one, int(d.ID)),
		}
		sli = append(sli, &two)
	}
	return sli
}

type ReturnLsGoodsClass struct {
	global.GVA_MODEL
	ClassName *string               `json:"className" form:"className" gorm:"column:class_name;comment:分类名称;size:20;" binding:"required"` //分类名称
	Pid       *int                  `json:"pid" form:"pid" gorm:"column:pid;comment:0 代表顶级;size:10;"`                                     //0 代表顶级
	ClassIcon *string               `json:"classIcon" form:"classIcon" gorm:"column:class_icon;comment:分类图标;"`                            //分类图标
	Sort      *int                  `json:"sort" form:"sort" gorm:"column:sort;comment:排序;size:10;"`                                      //排序
	Enable    *string               `json:"enable" form:"enable" gorm:"index;column:enable;comment:0 不展示 1展示;"`                           //0 不展示 1展示
	CreatedBy uint                  `gorm:"column:created_by;comment:创建者"`
	UpdatedBy uint                  `gorm:"column:updated_by;comment:更新者"`
	DeletedBy uint                  `gorm:"column:deleted_by;comment:删除者"`
	Children  []*ReturnLsGoodsClass `json:"children" gorm:"-"`
}
